package com.leon.datalink.node.response;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * author: zyl
 * date: 2025//09 16:53:31
 */
@Slf4j
public class ResponseManager {

    private static final Cache<String, Object> responseMap = CacheBuilder.newBuilder()
            .expireAfterWrite(30, TimeUnit.SECONDS)
            .maximumSize(1000)
            .removalListener(notification -> {
                log.info("Expired requestId: " + notification.getKey());
            })
            .build();

    public static void add(String requestId, Object responseHandler) {
        responseMap.put(requestId, responseHandler);
    }

    public static Object get(String requestId) {
        return responseMap.asMap().remove(requestId);
    }
}
